#delimit ;
set more off;
pause on;
clear;

/*****************************************************************************/
/* ABOUT THIS FILE */
/*****************************************************************************/

/*
OVERVIEW
The purpose of this file is to compile all the variables that will be used in the analysis.
It merges ethnic and demographic, candidate, regional, and poster appeal data.

CONTENTS
1. RELIGIOUS DEMOGRAPHIC VARS: RFI EFI AND REL GRP % AND NUMBER SIZES
2. ETHNIC DEMOGRAPHIC VARS: EFI EPI AND ETHNIC GRP % SIZES
3. MERGE CANDIDATE, DEMOGRAPHIC, AND REGIONAL VAR FILES
4. IDENTIFY HEAD'S ETHNIC AND RELIGIOUS GROUP SIZE
5. MERGE WITH POSTER APPEALS & MAKE BONDING, BRIDGING, BYPASSING APPEALS
6. CREATE OTHER IVs
7. KEEP ORDER AND SAVE DATABASE FILE (UOA = Poster N = 1,504)
*/

/*****************************************************************************/
/* 1. RELIGIOUS DEMOGRAPHIC VARS: RFI EFI AND REL GRP % AND NUMBER SIZES */
/*****************************************************************************/

insheet using "csv/regional_religion.csv"; // obs = a district
keep dis_code islam kristen katolik hindu budha khonghucu lainnya totpop;

/* RESHAPE WIDE TO LONG: REL GRP AS OB (same as in the ethnicity file) ------*/
rename islam rel1;
rename kristen rel2;
rename katolik rel3;
rename hindu rel4;
rename budha rel5;
rename khonghucu rel6;
rename lainnya rel7;
reshape long rel, i(dis_code) j(code_rgrp);
rename rel bps_num_rgrp;
bysort dis_code: egen bps_dispop_rel = sum(bps_num_rgrp); // Total district pop var.
gen bps_pct_rgrp = (bps_num_rgrp/bps_dispop_rel) * 100; // % Rel grp in district var

/* 1. RELIGIOUS-FRACTIONALIZTION INDEX: 1-sum(pct_grp^2)---------------------*/
gen bps_pct_rgrp_sqr = (bps_pct_rgrp/100)^2; // Make rel grp squared
replace bps_pct_rgrp_sqr = 0 if bps_pct_rgrp == 0;
egen rfi = sum(bps_pct_rgrp_sqr), by(dis_code); // sum grps
replace rfi = 1-rfi; // minus 1
drop bps_pct_rgrp_sqr;

/* 2. POLARIZATION MEASURE: 1-sum((0.5-pct)/0.5)^2 * pct) (Reynol-Querols)---*/
gen bps_pct_rgrp2 = bps_pct_rgrp /100; // Make it a proportion
gen rpi_a = (((0.5 - bps_pct_rgrp2) /0.5) * ((0.5 - bps_pct_rgrp2) /0.5)) * bps_pct_rgrp2; // ((0.5-pct)/0.5)^2 * pct)
egen rpi = sum(rpi_a), by(dis_code); // sum grps
replace rpi = 1-rpi; // minus 1
drop rpi_a bps_pct_rgrp2;

/* RANK GRPS AND RESHAPE  ---------------------------------------------------*/
drop if code_rgrp == 7; // Drop the small 'other' religious category
egen rank = rank(-bps_pct_rgrp), unique by(dis_code); // Rank rel grps in district (ties broken)
label define labels_rgrp 1 "Islam" 2 "kristen" 3 "Katolik" 4"Hindu" 5 "Budha" 6 "Khonghucu" 7 "Rel_Lainnya", modify;
label values code_rgrp* labels_rgrp; // label rel grps
reshape wide bps_num_rgrp code_rgrp bps_pct_rgrp, i(dis_code) j(rank); // Reshape long to wide (Dis as obs)

/* 3. CREATE VAR FOR EACH RELIGIOUS GROUP % SIZE  ---------------------------*/
gen islam_pop = bps_pct_rgrp1 if code_rgrp1 == 1;
replace islam_pop = bps_pct_rgrp2 if code_rgrp2 == 1;
replace islam_pop = bps_pct_rgrp3 if code_rgrp3 == 1;
replace islam_pop = bps_pct_rgrp3 if code_rgrp4 == 1;
replace islam_pop = bps_pct_rgrp3 if code_rgrp5 == 1;
replace islam_pop = bps_pct_rgrp3 if code_rgrp6 == 1;
gen kristen_pop = bps_pct_rgrp1 if code_rgrp1 == 2;
replace kristen_pop = bps_pct_rgrp2 if code_rgrp2 == 2;
replace kristen_pop = bps_pct_rgrp3 if code_rgrp3 == 2;
replace kristen_pop = bps_pct_rgrp4 if code_rgrp4 == 2;
replace kristen_pop = bps_pct_rgrp5 if code_rgrp5 == 2;
replace kristen_pop = bps_pct_rgrp6 if code_rgrp6 == 2;
gen katolik_pop = bps_pct_rgrp1 if code_rgrp1 == 3;
replace katolik_pop = bps_pct_rgrp2 if code_rgrp2 == 3;
replace katolik_pop = bps_pct_rgrp3 if code_rgrp3 == 3;
replace katolik_pop = bps_pct_rgrp4 if code_rgrp4 == 3;
replace katolik_pop = bps_pct_rgrp5 if code_rgrp5 == 3;
replace katolik_pop = bps_pct_rgrp6 if code_rgrp6 == 3;
gen hindu_pop = bps_pct_rgrp1 if code_rgrp1 == 4;
replace hindu_pop = bps_pct_rgrp2 if code_rgrp2 == 4;
replace hindu_pop = bps_pct_rgrp3 if code_rgrp3 == 4;
replace hindu_pop = bps_pct_rgrp4 if code_rgrp4 == 4;
replace hindu_pop = bps_pct_rgrp5 if code_rgrp5 == 4;
replace hindu_pop = bps_pct_rgrp6 if code_rgrp6 == 4;
gen budha_pop = bps_pct_rgrp1 if code_rgrp1 == 5;
replace budha_pop = bps_pct_rgrp2 if code_rgrp2 == 5;
replace budha_pop = bps_pct_rgrp3 if code_rgrp3 == 5;
replace budha_pop = bps_pct_rgrp4 if code_rgrp4 == 5;
replace budha_pop = bps_pct_rgrp5 if code_rgrp5 == 5;
replace budha_pop = bps_pct_rgrp6 if code_rgrp6 == 5;
gen khonghucu_pop = bps_pct_rgrp1 if code_rgrp1 == 6;
replace khonghucu_pop = bps_pct_rgrp2 if code_rgrp2 == 6;
replace khonghucu_pop = bps_pct_rgrp3 if code_rgrp3 == 6;
replace khonghucu_pop = bps_pct_rgrp4 if code_rgrp4 == 6;
replace khonghucu_pop = bps_pct_rgrp5 if code_rgrp5 == 6;
replace khonghucu_pop = bps_pct_rgrp6 if code_rgrp6 == 6;

/* 3. RELIGIOUS GROUP NUMBER SIZES  -----------------------------------------*/
gen islam_pop_num = bps_num_rgrp1 if code_rgrp1 == 1;
replace islam_pop_num = bps_num_rgrp2 if code_rgrp2 == 1;
replace islam_pop_num = bps_num_rgrp3 if code_rgrp3 == 1;
replace islam_pop_num = bps_num_rgrp3 if code_rgrp4 == 1;
replace islam_pop_num = bps_num_rgrp3 if code_rgrp5 == 1;
replace islam_pop_num = bps_num_rgrp3 if code_rgrp6 == 1;
gen kristen_pop_num = bps_num_rgrp1 if code_rgrp1 == 2;
replace kristen_pop_num = bps_num_rgrp2 if code_rgrp2 == 2;
replace kristen_pop_num = bps_num_rgrp3 if code_rgrp3 == 2;
replace kristen_pop_num = bps_num_rgrp4 if code_rgrp4 == 2;
replace kristen_pop_num = bps_num_rgrp5 if code_rgrp5 == 2;
replace kristen_pop_num = bps_num_rgrp6 if code_rgrp6 == 2;
gen katolik_pop_num = bps_num_rgrp1 if code_rgrp1 == 3;
replace katolik_pop_num = bps_num_rgrp2 if code_rgrp2 == 3;
replace katolik_pop_num = bps_num_rgrp3 if code_rgrp3 == 3;
replace katolik_pop_num = bps_num_rgrp4 if code_rgrp4 == 3;
replace katolik_pop_num = bps_num_rgrp5 if code_rgrp5 == 3;
replace katolik_pop_num = bps_num_rgrp6 if code_rgrp6 == 3;
gen hindu_pop_num = bps_num_rgrp1 if code_rgrp1 == 4;
replace hindu_pop_num = bps_num_rgrp2 if code_rgrp2 == 4;
replace hindu_pop_num = bps_num_rgrp3 if code_rgrp3 == 4;
replace hindu_pop_num = bps_num_rgrp4 if code_rgrp4 == 4;
replace hindu_pop_num = bps_num_rgrp5 if code_rgrp5 == 4;
replace hindu_pop_num = bps_num_rgrp6 if code_rgrp6 == 4;
gen budha_pop_num = bps_num_rgrp1 if code_rgrp1 == 5;
replace budha_pop_num = bps_num_rgrp2 if code_rgrp2 == 5;
replace budha_pop_num = bps_num_rgrp3 if code_rgrp3 == 5;
replace budha_pop_num = bps_num_rgrp4 if code_rgrp4 == 5;
replace budha_pop_num = bps_num_rgrp5 if code_rgrp5 == 5;
replace budha_pop_num = bps_num_rgrp6 if code_rgrp6 == 5;
gen khonghucu_pop_num = bps_num_rgrp1 if code_rgrp1 == 6;
replace khonghucu_pop_num = bps_num_rgrp2 if code_rgrp2 == 6;
replace khonghucu_pop_num = bps_num_rgrp3 if code_rgrp3 == 6;
replace khonghucu_pop_num = bps_num_rgrp4 if code_rgrp4 == 6;
replace khonghucu_pop_num = bps_num_rgrp5 if code_rgrp5 == 6;
replace khonghucu_pop_num = bps_num_rgrp6 if code_rgrp6 == 6;

save "dta/regional_religion.dta", replace;
clear;

/*****************************************************************************/
/* 2. ETHNIC DEMOGRAPHIC VARS: EFI EPI AND ETHNIC GRP % SIZES */
/*****************************************************************************/

insheet using "csv/regional_ethnicity.csv"; // obs = a district

/* 1. ETHNO-FRACTIONALIZTION INDEX: 1-sum(pct_grp^2)---------------------*/
gen bps_pct_egrp_sqr = (bps_pct_egrp/100)^2; // Make rel grp squared
replace bps_pct_egrp_sqr = 0 if bps_pct_egrp == 0;
egen efi = sum(bps_pct_egrp_sqr), by(dis_code);  // sum grps
replace efi = 1-efi;  // minus 1
drop bps_pct_egrp_sqr;

/* 2. POLARIZATION MEASURE: 1-sum((0.5-pct)/0.5)^2 * pct) (Reynol-Querols)---*/
gen bps_pct_egrp2 = bps_pct_egrp /100; // Make it a proportion
gen epi_a = (((0.5 - bps_pct_egrp2) /0.5) * ((0.5 - bps_pct_egrp2) /0.5)) * bps_pct_egrp2; // ((0.5-pct)/0.5)^2 * pct)
egen epi = sum(epi_a), by(dis_code);  // sum grps
replace epi = 1-epi;  // minus 1
drop epi_a bps_pct_egrp2;

/* RANK GRPS AND RESHAPE  ---------------------------------------------------*/
egen rank = rank(-bps_pct_egrp), unique by(dis_code); // Rank rel grps in district (ties broken)
drop bps_num_egrp;
drop bps_name_egrp;
reshape wide bps_code_egrp bps_pct_egrp, i(dis_code) j(rank); // Reshape long to wide (Dis as obs)

/* 3. KEEP THE LARGEST 10 GROUPS AND THEIR % SIZES */
keep dis_code bps_dispop_eth efi epi
bps_code_egrp1-bps_code_egrp25
bps_pct_egrp1-bps_pct_egrp25;

save "dta/regional_ethnicity.dta", replace;
clear;

/*****************************************************************************/
/* 3. MERGE CANDIDATE DEMOGRAPHIC AND REGIONAL VAR FILES */
/*****************************************************************************/

/* SAVE CSV FILES AS DTA ----------------------------------------------------*/
insheet using "csv/candidates.csv"; // a candidate team = ob
save "dta/candidates.dta", replace;
clear;
insheet using "csv/regional.csv"; // an electoral district = ob
save "dta/regional.dta", replace;
clear;

/* MERGE FILES  -------------------------------------------------------------*/
use "dta/candidates.dta";
merge m:1 dis_code using "dta/regional_ethnicity.dta";
drop if _merge == 2; // districts that we have no candidates from
drop _merge;
merge m:1 dis_code using "dta/regional_religion.dta";
drop if _merge == 2; // districts that we have no candidates from
drop _merge;
merge m:1 dis_code using "dta/regional.dta";
drop if _merge == 2; // districts that we have no candidates from
drop _merge;

/*****************************************************************************/
/* 4. IDENTIFY HEAD'S ETHNIC AND RELIGIOUS GROUP SIZE */
/*****************************************************************************/

/* IDENTIFY HEAD CANDIDATE'S ETHNIC GROUP SIZE ------------------------------*/
local count10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;
gen size_h_egrp = .;
foreach x in `count10'{;
replace size_h_egrp = bps_pct_egrp`x'
if h_bps_code_egrp == bps_code_egrp`x';
};
replace size_h_egrp = 0.0683524 if can == "dis_head_1273_10"; // Barkat Shah Pakistani ethnic group was very small and had been excluded
replace size_h_egrp = 0.01 if can == "dis_head_1275_9"; // H. Ajib Shah Pakistani same as above

/* IDENTIFY HEAD CANDIDATE'S RELIGIOUS GROUP SIZE ---------------------------*/
/* Make Protestant/Catholic candidate's religion Christian */
replace h_rgrp = "kriskat" if h_rgrp == "kristen";
replace h_rgrp = "kriskat" if h_rgrp == "katolik";
replace dp_rgrp = "kriskat" if dp_rgrp == "kristen";
replace dp_rgrp = "kriskat" if dp_rgrp == "katolik";
gen size_h_rgrp = .;
replace size_h_rgrp = islam_pop if h_rgrp == "islam";
replace size_h_rgrp = kristen_pop + katolik_pop if h_rgrp == "kriskat";
replace size_h_rgrp = hindu_pop if h_rgrp == "hindu";
replace size_h_rgrp = budha_pop if h_rgrp == "budha";
replace size_h_rgrp = khonghucu_pop if h_rgrp == "khonghucu";

/*****************************************************************************/
/* 5. MERGE WITH POSTER APPEALS & MAKE BONDING, BRIDGING, BYPASSING APPEALS */
/*****************************************************************************/

merge 1:m can_id using "dta/appeals.dta";
drop _merge;

/* RELIGIOUS BRIDGING AND BONDING BASED ON CANID ----------------------------*/
/* Cross religious bridging (groups the candidate does not belong to) */
gen cr_rel = cr_rel_b; // A poster with appeals to multiple religions
replace cr_rel = 1 if any_rel_is_b == 1 & h_rgrp != "islam"; // poster with only Islamic appeals by a non-Islamic candidate
replace cr_rel = 1 if any_rel_ch_b == 1 & h_rgrp != "kriskat"; // same logic, Christian
replace cr_rel = 1 if any_rel_bu_b == 1 & h_rgrp != "budha"; // same logic, Buddhist
replace cr_rel = 1 if any_rel_hi_b == 1 & h_rgrp != "hindu"; // same logic, Hindu

/* Broad religious bridging (appeals to religiosity) ------------------------*/
gen re_rel = re_rel_b;

/* Religious Bridging = cross + broad bridging ------------------------------*/
gen recr_rel = 0;
replace recr_rel = 1 if cr_rel == 1;
replace recr_rel = 1 if re_rel == 1;

/* Religious Bonding (posters with an appeal to 1 religion but are not bridging */
gen sa_rel = sa_rel_b;
replace sa_rel = 0 if recr_rel == 1;

/* Religious Bypassing ------------------------------------------------------*/
gen non_rel = 1;
replace non_rel = 0 if sa_rel == 1;
replace non_rel = 0 if recr_rel == 1;

/* ETHNIC BRIDGING AND BONDING BASED ON CANID -------------------------------*/
/* Cross ethnic bridging (groups the candidate does not belong to) */
gen cr_eth = cr_eth_b;
replace cr_eth = 1 if any_eth_acehnese_b == 1 	& h_bps_code_egrp != 1;
replace cr_eth = 1 if any_eth_ambon_b == 1 			& h_bps_code_egrp != 822;
replace cr_eth = 1 if any_eth_bajawa_b == 1 		& h_bps_code_egrp != 136;
replace cr_eth = 1 if any_eth_banjar_b == 1 		& h_bps_code_egrp != 520;
replace cr_eth = 1 if any_eth_balinese_b == 1 	& h_bps_code_egrp != 124;
replace cr_eth = 1 if any_eth_betawi_b == 1 		& h_bps_code_egrp != 111;
replace cr_eth = 1 if any_eth_bugis_b == 1 			& h_bps_code_egrp != 695;
replace cr_eth = 1 if any_eth_chinese_b == 1 		& h_bps_code_egrp != 1319;
replace cr_eth = 1 if any_eth_javanese_b == 1 	& h_bps_code_egrp != 114;
replace cr_eth = 1 if any_eth_karo_b == 1 			& h_bps_code_egrp != 15;
replace cr_eth = 1 if any_eth_lampung_b == 1 		& h_bps_code_egrp != 88;
replace cr_eth = 1 if any_eth_luwu_b == 1 			& h_bps_code_egrp != 705;
replace cr_eth = 1 if any_eth_madurese_b == 1 	& h_bps_code_egrp != 121;
replace cr_eth = 1 if any_eth_makassar_b == 1 	& h_bps_code_egrp != 706;
replace cr_eth = 1 if any_eth_malay_b == 1 	& h_bps_code_egrp != 23;
replace cr_eth = 1 if any_eth_malay_b == 1 	& h_bps_code_egrp != 24;
replace cr_eth = 1 if any_eth_malay_b == 1 	& h_bps_code_egrp != 107;
replace cr_eth = 1 if any_eth_manggarai_b == 1 	& h_bps_code_egrp != 177;
replace cr_eth = 1 if any_eth_minahasa_b == 1 	& h_bps_code_egrp != 609;
replace cr_eth = 1 if any_eth_minangkabau_b == 1 & h_bps_code_egrp != 32;
replace cr_eth = 1 if any_eth_nagekeo_b == 1 		& h_bps_code_egrp != 185;
replace cr_eth = 1 if any_eth_ngada_b == 1 			& h_bps_code_egrp != 184;
replace cr_eth = 1 if any_eth_nias_b == 1 			& h_bps_code_egrp != 25;
replace cr_eth = 1 if any_eth_sekadau_b == 1 		& h_bps_code_egrp != 489;
replace cr_eth = 1 if any_eth_simalungun_b == 1 & h_bps_code_egrp != 18;
replace cr_eth = 1 if any_eth_sumba_b == 1 			& h_bps_code_egrp != 563;
replace cr_eth = 1 if any_eth_sundanese_b == 1 	& h_bps_code_egrp != 113;
replace cr_eth = 1 if any_eth_toba_b == 1 			& h_bps_code_egrp != 20;
replace cr_eth = 1 if any_eth_toraja_b == 1 		& h_bps_code_egrp != 665;

/* Broad ethnic bridging (appeals to ethnicity) -----------------------------*/
gen re_eth = re_eth_b;

/* Ethnic Bridging = cross + broad bridging ---------------------------------*/
gen recr_eth = 0;
replace recr_eth = 1 if cr_eth == 1;
replace recr_eth = 1 if re_eth == 1;

/* Ethnic Bonding (posters with an appeal to 1 religion but are not bridging */
gen sa_eth = sa_eth_b;
replace sa_eth = 0 if recr_eth == 1;

/* Ethnic Bypassing ---------------------------------------------------------*/
gen non_eth = 1;
replace non_eth = 0 if sa_eth == 1;
replace non_eth = 0 if recr_eth == 1;

/*****************************************************************************/
/* 6. CREATE OTHER IVs */
/*****************************************************************************/

/* VIABLE RELIGIOUS & ETHNIC GROUPS -----------------------------------------*/
gen h_rgrp_viable = 1 if size_h_rgrp > 50;
replace h_rgrp_viable = 0 if size_h_rgrp < 50;
replace h_rgrp_viable = 0 if h_rgrp !=  "islam";
gen h_egrp_viable = 1 if size_h_egrp > 50;
replace h_egrp_viable = 0 if size_h_egrp < 50;

/* Viable religious group continous var */
gen size_h_rgrp_nonislam0 = size_h_rgrp;
replace size_h_rgrp_nonislam0 = 0 if h_rgrp != "islam";

/* Majority Religious Group -------------------------------------------------*/
gen h_rgrp_maj = 1 if size_h_rgrp > 50;
replace h_rgrp_maj = 0 if size_h_rgrp < 50;

/* ECONOMIC VARS ------------------------------------------------------------*/
gen poverty			= poverty / (pop_jl08); // % in poverty
gen farm_grdp		= farmfish_grdp; // x $100,000s
gen farm_grdp_pct 	= farmfish_grdp / total_grdp; // % of GRDP from farming & fishing

/* ATTACHMENT VARS (ETHNIC LAW & CANDIDATE SPECIFIC HOUSE OF WORSHIP)--------*/
gen eth_law 		= adat_vil_n / villages_03; // % district villages with Ethnic Law (2003)
gen mosque_1000		= (mosques_n * 1000) / islam_pop_num; // Mosques only (2008)
gen church_1000 = ((prochurch_n + cathchurch_n) * 1000) / (kristen_pop_num + katolik_pop_num); // (2008)
gen budtemple_1000 = (budtemple_n * 1000) / budha_pop_num; // (2008)
gen hintemple_1000 = (hintemple_n * 1000) / hindu_pop_num; // (2008)
gen pl_worship_1000_cangrp = 0;
replace pl_worship_1000_cangrp = mosque_1000 if h_rgrp == "islam";
replace pl_worship_1000_cangrp = church_1000 if h_rgrp == "kriskat";
replace pl_worship_1000_cangrp = budtemple_1000 if h_rgrp == "budha";
replace pl_worship_1000_cangrp = hintemple_1000 if h_rgrp == "hindu";

/* POPULATION ---------------------------------------------------------------*/
gen pop_log			= log(totpop); // 2010 figure from religion, includes nonrespondents

/* FOUR CANDIDATE TYPES -----------------------------------------------------*/
gen can_d_bond = 0;
gen can_i_bond = 0;
gen can_r_bond = 0;
gen can_d_bridge = 0;
replace can_d_bond = 1 if h_egrp_viable == 1 & h_rgrp_viable == 1;
replace can_i_bond = 1 if h_egrp_viable == 1 & h_rgrp_viable == 0;
replace can_r_bond = 1 if h_egrp_viable == 0 & h_rgrp_viable == 1;
replace can_d_bridge = 1 if h_egrp_viable == 0 & h_rgrp_viable == 0;
gen can_d_maj = 0;
replace can_d_maj = 1 if h_egrp_viable == 1 & h_rgrp_maj == 1;

/* POLITICAL PARTY SUPPORT --------------------------------------------------*/
replace parties = lower(parties);
gen one_party = 0; // Candidates supported by only one party
replace one_party = 1 if parties =="golkar";
replace one_party = 1 if parties =="hanura";
replace one_party = 1 if parties =="pan";
replace one_party = 1 if parties =="pbr";
replace one_party = 1 if parties =="pd";
replace one_party = 1 if parties =="PDIP";
replace one_party = 1 if parties =="PDK";
replace one_party = 1 if parties =="pdemokrat";
replace one_party = 1 if parties =="pkb";
replace one_party = 1 if parties =="pprn";

gen party_regh_data = ""; // candidates with missing party data
replace party_regh_data = "missing" if parties == "";

/* Has Islamic party support */
gen party_regh_is = 0 if party_regh_data != "missing";
replace party_regh_is = 1 if (regexm(parties, "ppnui"));
replace party_regh_is = 1 if (regexm(parties, "pbb"));
replace party_regh_is = 1 if (regexm(parties, "pbr"));
replace party_regh_is = 1 if (regexm(parties, "pks"));
replace party_regh_is = 1 if (regexm(parties, "pppembangunan"));

/* Has moderate Islamic party support (Nationalit-Islamic parties)*/
gen party_regh_natis = 0 if party_regh_data != "missing";
replace party_regh_natis = 1 if (regexm(parties, "pan"));
replace party_regh_natis = 1 if (regexm(parties, "pkb"));
replace party_regh_natis = 1 if (regexm(parties, "pknu"));

/* Has nationalist party support */
gen party_regh_nat = 0 if party_regh_data != "missing";
replace party_regh_nat = 1 if (regexm(parties, "buruh"));
replace party_regh_nat = 1 if (regexm(parties, "pkedaulatan"));
replace party_regh_nat = 1 if (regexm(parties, "pkperjuangan"));
replace party_regh_nat = 1 if (regexm(parties, "pkpi"));
replace party_regh_nat = 1 if (regexm(parties, "pmerdeka"));
replace party_regh_nat = 1 if (regexm(parties, "pmb"));
replace party_regh_nat = 1 if (regexm(parties, "pni"));
replace party_regh_nat = 1 if (regexm(parties, "ppelopor"));
replace party_regh_nat = 1 if (regexm(parties, "ppdaerah"));
replace party_regh_nat = 1 if (regexm(parties, "ppdi"));
replace party_regh_nat = 1 if (regexm(parties, "ppindonesia"));
replace party_regh_nat = 1 if (regexm(parties, "ppib"));
replace party_regh_nat = 1 if (regexm(parties, "pppi"));
replace party_regh_nat = 1 if (regexm(parties, "psi"));
replace party_regh_nat = 1 if (regexm(parties, "gerindra"));
replace party_regh_nat = 1 if (regexm(parties, "hanura"));
replace party_regh_nat = 1 if (regexm(parties, "pbn"));
replace party_regh_nat = 1 if (regexm(parties, "pdemokrat"));
replace party_regh_nat = 1 if (regexm(parties, "pdip"));
replace party_regh_nat = 1 if (regexm(parties, "pdk"));
replace party_regh_nat = 1 if (regexm(parties, "pdp"));
replace party_regh_nat = 1 if (regexm(parties, "pds"));
replace party_regh_nat = 1 if (regexm(parties, "pis"));
replace party_regh_nat = 1 if (regexm(parties, "pkd"));
replace party_regh_nat = 1 if (regexm(parties, "pkpb"));
replace party_regh_nat = 1 if (regexm(parties, "pnbk"));
replace party_regh_nat = 1 if (regexm(parties, "ppatriot"));
replace party_regh_nat = 1 if (regexm(parties, "pprn"));
replace party_regh_nat = 1 if (regexm(parties, "republikan"));
replace party_regh_nat = 1 if (regexm(parties, "golkar"));

/* 1. Is an independent candidate */
gen     party_regh_ind = 0 if party_regh_data != "missing";
replace party_regh_ind = 1 if (regexm(parties, "independent"));

/* 2. Is supported by only Islamic parties */
gen     party_regh_isonly = 0 if party_regh_data != "missing";
replace party_regh_isonly = 1 if party_regh_is == 1 & party_regh_natis == 0 & party_regh_nat == 0 & party_regh_ind == 0;

/* 3. Is supported by only nationalist parties  */
gen     party_regh_natonly = 0 if party_regh_data != "missing";
replace party_regh_natonly = 1 if party_regh_is == 0 & party_regh_natis == 0 & party_regh_nat == 1 & party_regh_ind == 0;
replace party_regh_natonly = 1 if party_regh_is == 0 & party_regh_natis == 1 & party_regh_nat == 0 & party_regh_ind == 0;
replace party_regh_natonly = 1 if party_regh_is == 0 & party_regh_natis == 1 & party_regh_nat == 1 & party_regh_ind == 0;

/* 4. Is supported by both nationalist and Islamic parties  */
gen     party_regh_natiscoal = 0 if party_regh_data != "missing";
replace party_regh_natiscoal = 1 if party_regh_is == 1 & party_regh_natis == 1 & party_regh_nat == 0 & party_regh_ind == 0;
replace party_regh_natiscoal = 1 if party_regh_is == 1 & party_regh_natis == 0 & party_regh_nat == 1 & party_regh_ind == 0;
replace party_regh_natiscoal = 1 if party_regh_is == 1 & party_regh_natis == 1 & party_regh_nat == 1 & party_regh_ind == 0;

/*****************************************************************************/
/* 7. KEEP ORDER AND SAVE DATABASE FILE (UOA = Poster N = 1,504) */
/*****************************************************************************/

local vars
sa_rel sa_eth
re_rel re_eth
recr_rel recr_eth
non_eth non_rel;

foreach var in `vars'{;
	replace `var' = `var' * 100;
	recast int `var', force;
};

local vars2
pos_id can_id reg_id // Indexing IDs
sa_rel re_rel recr_rel non_rel sa_eth re_eth recr_eth non_eth // DVs
h_rgrp_viable h_egrp_viable size_h_rgrp_nonislam0 // IVs
eth_law pl_worship_1000_cangrp poverty farm_grdp_pct num_teams // IVs
female_can pop_log // CVs
dis_code prov_no // Constituency Vars
islam_pop bps_pct_egrp1 efi rfi epi rpi // Demographic Vars
h_rgrp h_egrp h_bps_code_egrp size_h_egrp size_h_rgrp // Candidate Identity
can_d_bond can_i_bond can_r_bond can_d_bridge can_d_maj // Candidate Types
parties one_party party_regh_is party_regh_isonly party_regh_natonly party_regh_natiscoal party_regh_ind // Parties
clothing1 clothing2 clothing1_party clothing2_party partysup bgpartylogo party_elites; // Party Symbolism

keep `vars2';
order `vars2';
save "dta/db.dta", replace;

erase "dta/candidates.dta";
erase "dta/regional.dta";
erase "dta/regional_religion.dta";
erase "dta/regional_ethnicity.dta";

/* END OF FILE ***************************************************************/
